package com.chihihx.launcher.ui.dialog

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.core.graphics.drawable.toDrawable
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.HORIZONTAL
import com.chihihx.launcher.bean.HomeTabDto
import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.databinding.DialogMainTopBinding
import com.nova.android.tvlauncher.databinding.ItemHomeTabBinding
import com.nova.android.tvlauncher.ext.clickNoRepeatWithAnim
import com.nova.android.tvlauncher.ext.scaleAnimation
import com.nova.android.tvlauncher.ui.dialog.base.BaseDialogFragment
import com_github_lib_base.ext.rv.setup
import com_github_lib_base.ext.width

class MainTopDialog : BaseDialogFragment<DialogMainTopBinding>() {

    override val dialogGravity: Int = Gravity.TOP // 显示在顶部

    override val dialogBackground: Drawable =
        "#80000000".toColorInt().toDrawable() // 半透明黑色背景

    override val dialogDimAmount: Float = 0.3f // 更低的背景模糊度

    override val dialogAnimation: Int = R.style.DialogAnimationTop // 使用从顶部滑下的动画

    override val dialogWidth: Int = ViewGroup.LayoutParams.MATCH_PARENT // 固定宽度（单位：像素）


    override fun initView(savedInstanceState: Bundle?) {
        initRV()
    }

    private fun initRV() {
        val tabs = arrayListOf(
            HomeTabDto(
                "设置",
                R.drawable.icon_setting,
                R.drawable.icon_setting_foc
            ),
            HomeTabDto(
                "设置",
                R.drawable.icon_setting,
                R.drawable.icon_setting_foc
            ),
            HomeTabDto(
                "设置",
                R.drawable.icon_setting,
                R.drawable.icon_setting_foc
            ),
            HomeTabDto(
                "设置",
                R.drawable.icon_setting,
                R.drawable.icon_setting_foc
            ),
            HomeTabDto(
                "设置",
                R.drawable.icon_setting,
                R.drawable.icon_setting_foc
            ),
            HomeTabDto(
                "设置",
                R.drawable.icon_setting,
                R.drawable.icon_setting_foc
            ),

            )

        binding.rvTab.layoutManager = LinearLayoutManager(context,HORIZONTAL,false)
        binding.rvTab.setup(tabs) {
            addType(HomeTabDto::class, R.layout.item_home_tab)
            onBind {

                val itemBinding = getBinding<ItemHomeTabBinding>()
                val item = getModel<HomeTabDto>()
                itemBinding.tvName.text = item.tabName
                itemBinding.ivIcon.setImageResource(item.iconRes)


                itemBinding.root.setOnFocusChangeListener { _, hasFocus ->
                    itemBinding.tvName.isSelected = hasFocus
                    itemView.scaleAnimation(hasFocus)
                    if (hasFocus) {
                        itemBinding.tvName.isSelected = true
                        itemBinding.ivIcon.setImageResource(item.iconResFocus)
                    } else {
                        itemBinding.tvName.isSelected = false
                        itemBinding.ivIcon.setImageResource(item.iconRes)
                    }
                }

                itemView.clickNoRepeatWithAnim(shrinkScale = 0.96f) {
                    when (modelPosition) {
                        0 -> {}
                        1 -> {}
                        2 -> {}

                        3 -> {}

                        4 -> {}
                        5 -> {}

                    }
                }


                itemView.setOnKeyListener { _, keyCode, event ->
                    if (keyCode == KeyEvent.KEYCODE_DPAD_UP) return@setOnKeyListener true
                    return@setOnKeyListener false
                }

            }
        }

        //binding.rvTab.setNumRows(1)
        //binding.rvTab.setItemSpacing(16)



    }


}